capture log close
log using $log/AED_dataprep_03_outcome_mth, text replace
   
clear

set matsize 800
set more off

set trace off
   
/* ---------------------------------------------------------------- */
/* --- Generating monthly outcome variables for ALMP activities --- */
/* ---------------------------------------------------------------- */

local iebversion v901


forvalues year=1996/1998 {
	
		use $orig/AED_9310_v1, clear
		
		rename persnr iza_id
		rename begorig beg_orig
		rename endorig end_orig
		rename erwstat erw_stat
		rename tentgelt tag_entg
		
		
		/*Only keep ALMP activities*/
		#delimit;
		keep if quelle==3 | quelle==4 | quelle==5 | quelle==6 | quelle==8 | quelle==9 | quelle==10
			| quelle==11 | quelle==12 | quelle==13 | quelle==14 | quelle==15 | quelle==17
			| quelle==21| quelle==22 | quelle==24 | quelle==25 | quelle==26 | quelle==27
			| quelle==28 | quelle==29 |quelle==30;
		#delimit cr
						
		qui drop if end_orig<mdy(1,1,`year')
		qui drop if beg_orig>mdy(12,31,`year')
		
		
		keep iza_id beg_orig end_orig erw_stat tag_entg quelle end_plan
		
		
		sort iza_id beg_orig
		
		replace end_plan = . if end_plan < beg_orig
		
		set seed 123456789
		bys iza_id: gen random = uniform()
			
				
		foreach month in 01 02 03 04 05 06 07 08 09 10 11 12 {
		
		
			di "### `year' `month'"
			di c(current_time)
			if "`month'"=="01" { 
				local m "Jan"
				local d 31
			}	
			if "`month'"=="02" { 
				local m "Feb"
				local d 28
			}	
			if "`year'"=="1996" | "`year'"=="2000" | "`year'"=="2004" | "`year'"=="2008" {
			local d 29
			}
			/*Schaltjahre*/			
			if "`month'"=="03" { 
				local m "Mar"
				local d 31
			}	
			if "`month'"=="04" { 
				local m "Apr"
				local d 30
			}	
			if "`month'"=="05" { 
				local m "May"
				local d 31
			}
			if "`month'"=="06" { 
				local m "Jun"
				local d 30
			}
			if "`month'"=="07" { 
				local m "Jul"
				local d 31
			}	
			if "`month'"=="08" { 
				local m "Aug"
				local d 31
			}	
			if "`month'"=="09" { 
				local m "Sep"
				local d 30
			}
			if "`month'"=="10" { 
				local m "Oct"
				local d 31
			}	
			if "`month'"=="11" { 
				local m "Nov"
				local d 30
			}
			if "`month'"=="12" { 
				local m "Dec"
				local d 31
			}
			
			
			/*Identify the specific month*/
			#delimit;
			bys iza_id: gen marker = 1 if ((beg_orig>=d(1`m'`year')
			& beg_orig<=d(`d'`m'`year')) | (end_orig>=d(1`m'`year') & end_orig<=d(`d'`m'`year'))
			| (beg_orig<d(1`m'`year') & end_orig>d(`d'`m'`year')))
			;
			#delimit cr
			/*If ALMP activities > 1 within the same month, we order them by duration so that the longest dominates*/
			by iza_id: gen dauer = (end_orig-beg_orig)
			gsort iza_id marker beg_orig -dauer random
			by iza_id marker: gen n = _n
			drop dauer
			
			
			/*Keep up to 4 ALMP per months*/
			
			forvalue n = 1/4 {
			
			#delimit;
			qui by iza_id: gen iza_mth`n'_`year'`month'_0 = erw_stat  if marker==1 & n==`n';
			qui by iza_id: gen iza_mth`n'_beg_`year'`month'_0 = beg_orig if marker==1 & n==`n';
			qui by iza_id: gen iza_mth`n'_end_`year'`month'_0 = end_orig if marker==1 & n==`n';
			qui by iza_id: gen iza_mth`n'_endplan_`year'`month'_0 = end_plan if marker==1 & n==`n';
			qui by iza_id: gen iza_mth`n'_d_`year'`month'_0 = (d(`d'`m'`year')-beg_orig) if marker==1 & n==`n'
			               & end_orig > d(`d'`m'`year');
			qui by iza_id: replace iza_mth`n'_d_`year'`month'_0 = (end_orig-beg_orig) if marker==1 & n==`n'
			               & ((end_orig <= d(`d'`m'`year') & beg_orig>=d(1`m'`year'))
			               | (beg_orig<d(1`m'`year') & end_orig>d(`d'`m'`year')));
			qui by iza_id: replace iza_mth`n'_d_`year'`month'_0 = (end_orig-d(1`m'`year')) if marker==1 & n==`n'
			               & beg_orig < d(1`m'`year');
			qui by iza_id: replace iza_mth`n'_d_`year'`month'_0 = `d' if iza_mth`n'_d_`year'`month'_0>`d'
			                                                       & iza_mth`n'_d_`year'`month'_0~=. ;
			qui by iza_id: gen iza_mth`n'_dc_`year'`month'_0 = (end_orig-beg_orig) if marker==1 & n==`n';
			qui by iza_id: gen iza_mth`n'_plandc_`year'`month'_0 = (end_plan-beg_orig) if marker==1 & n==`n';
			#delimit cr
			/*Extending info for all observations per unit*/
			qui by iza_id: gen     iza_mth`n'_`year'`month'_temp = iza_mth`n'_`year'`month'_0
			qui by iza_id: replace iza_mth`n'_`year'`month'_temp = iza_mth`n'_`year'`month'_temp[_n-1] if iza_mth`n'_`year'`month'_0==.
			qui by iza_id: gen     iza_mth`n'_`year'`month'    = iza_mth`n'_`year'`month'_temp[_N]
			drop iza_mth`n'_`year'`month'_temp iza_mth`n'_`year'`month'_0
			foreach typ in beg end d dc endplan plandc{
				qui by iza_id: gen     iza_mth`n'_`typ'_`year'`month'_temp = iza_mth`n'_`typ'_`year'`month'_0
				qui by iza_id: replace iza_mth`n'_`typ'_`year'`month'_temp = iza_mth`n'_`typ'_`year'`month'_temp[_n-1] if iza_mth`n'_`typ'_`year'`month'_0==.
				qui by iza_id: gen     iza_mth`n'_`typ'_`year'`month'    = iza_mth`n'_`typ'_`year'`month'_temp[_N]
				drop iza_mth`n'_`typ'_`year'`month'_temp iza_mth`n'_`typ'_`year'`month'_0
			}
			format iza_mth`n'_beg_`year'`month' iza_mth`n'_end_`year'`month' %d
			}
			
			drop marker n
						
			} /* End: m (month) */
			
	bys iza_id: drop if _n>1
	/* Reduce Data */
	
	keep iza*
			
	sort iza_id
	
	compress
	save $data/outcome_mth_y`year'_`iebversion', replace
			
} /* End: year */





clear
set mem 500m
log close
